package com.example.rbacmanage.handler;

import com.alibaba.druid.support.json.JSONUtils;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 权限异常处理
 */
public class MyAccessDeniedHandler implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
        Map<String,Object> map = new HashMap<>();
        map.put("code",403);
        map.put("msg","权限不足");
        response.setCharacterEncoding("UTF-8");
        response.setHeader("Content-Type","application/json;charset=utf-8");
        response.getWriter().write(JSONUtils.toJSONString(map));
    }
}
